/*
 * Mainmenu.cpp
 *
 *  Created on: 19.11.2009
 *      Author: Sergej Schmidt
 */

#include "MainMenuState.h"

using namespace Ogre;

MainMenuState MainMenuState::m_pMainMenuState;

void MainMenuState::enter()
{
	m_pRoot = Root::getSingletonPtr();
	m_pRenderWindow = m_pRoot->getAutoCreatedWindow();
	m_pSceneMgr = GameManager::getSingleton().getSceneManager();
    m_pCamera = GameManager::getSingleton().getCamera();
    m_pViewport = GameManager::getSingleton().getViewport();

    m_pSceneMgr->setAmbientLight( ColourValue( 1, 1, 1 ) );
    m_pSceneMgr->setSkyDome(true, "Examples/CloudySky", 5, 8);

    // TERRAIN
    //m_pTerrain = static_cast<SceneNode*>(m_pSceneMgr->getRootSceneNode()->createChild());
    Entity*	ent2 = m_pSceneMgr->createEntity( "Terrain", "Plane.mesh" );
    m_pTerrain = m_pSceneMgr->getRootSceneNode()->createChildSceneNode("TerrainNode");
    m_pTerrain->attachObject(ent2);
    m_pTerrain->translate( 0,-100,0 );
    m_pTerrain->scale(1500,1000,1500);
    // PARTICLES
    m_pFountainNode = static_cast<SceneNode*>(m_pSceneMgr->getRootSceneNode()->createChild());
    ParticleSystem* pSys2 = m_pSceneMgr->createParticleSystem("fountain1", "Examples/Smoke");
    SceneNode* fNode = static_cast<SceneNode*>(m_pFountainNode->createChild());
    fNode->attachObject(pSys2);
    m_pFountainNode->setPosition( 300, 0, 0 );
    // Свет
    m_pLight = m_pSceneMgr->createLight("Light1");
    m_pLight->setType( Light::LT_POINT );
    m_pLight->setPosition( 2500, 350, 0 );
    // Камера
    m_pCamera->setPosition( 0, 200, -1000 );
    m_pCamera->lookAt( 0, 0, 0 );
    m_fGradus = 0.0f;
    // MyGUI INIT
    Gui_Initialisieren();

    // TODO DEBUG
    m_pItems = new Items();

    m_bExitGame = false;
}
void MainMenuState::exit()
{
	m_pSceneMgr->destroyAllParticleSystems();
	m_pSceneMgr->destroyAllEntities();
	if(m_pGui)
	{
		m_pGui->destroyAllChildWidget();
	}
	// TODO DEBUG
	delete m_pItems;
}

void MainMenuState::pause()
{
	// в меню нет смысла
}

void MainMenuState::resume()
{
	// в меню нет смысла
}

void MainMenuState::keyPressed(const OIS::KeyEvent& e)
{
	if(m_pGui) m_pGui->injectKeyPress( MyGUI::KeyCode::Enum(e.key), e.text );
}
void MainMenuState::keyReleased(const OIS::KeyEvent& e)
{
    if( e.key == OIS::KC_ESCAPE )
    {
        m_bExitGame = true;
    }
    if(m_pGui) m_pGui->injectKeyRelease( MyGUI::KeyCode::Enum(e.key) );
}

void MainMenuState::mouseMoved( const OIS::MouseEvent& e )
{
	if(m_pGui) m_pGui->injectMouseMove( e.state.X.abs, e.state.Y.abs, e.state.Z.abs );
}

void MainMenuState::mousePressed( const OIS::MouseEvent &e, OIS::MouseButtonID id )
{
	if(m_pGui) m_pGui->injectMousePress( e.state.X.abs, e.state.Y.abs, MyGUI::MouseButton::Enum(id) );
}

void MainMenuState::mouseReleased( const OIS::MouseEvent &e, OIS::MouseButtonID id )
{
	if(m_pGui) m_pGui->injectMouseRelease( e.state.X.abs, e.state.Y.abs, MyGUI::MouseButton::Enum(id) );
}

bool MainMenuState::frameStarted(const Ogre::FrameEvent& evt)
{
	// Вычисление и установка позиции камеры
	m_fGradus += evt.timeSinceLastFrame * 4;
	if(m_fGradus > 359)	m_fGradus -= 359;
	m_pCamera->setPosition( Math::Sin(Degree(m_fGradus))*1000, 200, Math::Cos(Degree(m_fGradus))*1000 );
	m_pCamera->lookAt( 0, 0, 0 );
	//
	if(m_pGui) m_pGui->injectFrameEntered( evt.timeSinceLastFrame );
	return true;
}

bool MainMenuState::frameEnded(const Ogre::FrameEvent& evt)
{
	if(m_bExitGame)
		return false;
	return true;
}

void MainMenuState::Gui_Initialisieren()
{
    m_pGui = GameManager::getSingleton().getGui();
    //
    m_pGui->load( "MainMenu.layout" );
    m_pGui->load( "NewGame.layout" );
    m_pGui->load( "LoadGame.layout" );
    m_pGui->load( "Option.layout" );
    m_pGui->load( "Credit.layout" );
    //
    m_pGui->findWidgetT( "wndMainmenu" )->setVisible(true);
    m_pGui->findWidgetT( "wndNewGame" )->setVisible(false);
    m_pGui->findWidgetT( "wndLoadGame" )->setVisible(false);
    m_pGui->findWidgetT( "wndOption" )->setVisible(false);
    m_pGui->findWidgetT( "wndCredit" )->setVisible(false);
    m_pGui->showPointer();

    // Главное меню
    _button = m_pGui->findWidget<MyGUI::Button>( "btNewGame" );
    _button->eventMouseButtonClick = MyGUI::newDelegate( this, &MainMenuState::OnClickNewGame );
    _button = m_pGui->findWidget<MyGUI::Button>( "btLoadGame" );
    _button->eventMouseButtonClick = MyGUI::newDelegate( this, &MainMenuState::OnClickLoadGame );
    _button = m_pGui->findWidget<MyGUI::Button>( "btOption" );
    _button->eventMouseButtonClick = MyGUI::newDelegate( this, &MainMenuState::OnClickOption );
    _button = m_pGui->findWidget<MyGUI::Button>( "btCredit" );
    _button->eventMouseButtonClick = MyGUI::newDelegate( this, &MainMenuState::OnClickCredit );
    _button = m_pGui->findWidget<MyGUI::Button>( "btQuit" );
    _button->eventMouseButtonClick = MyGUI::newDelegate( this, &MainMenuState::OnClickQuit );
    // Новая Игра
    _button = m_pGui->findWidget<MyGUI::Button>( "btNewGameOk" );
    _button->eventMouseButtonClick = MyGUI::newDelegate( this, &MainMenuState::OnClickNewGameOkOrCancel );
    _button = m_pGui->findWidget<MyGUI::Button>( "btNewGameCancel" );
    _button->eventMouseButtonClick = MyGUI::newDelegate( this, &MainMenuState::OnClickNewGameOkOrCancel );
    // Загрузить Игру
    _button = m_pGui->findWidget<MyGUI::Button>( "btLoadGameOk" );
    _button->eventMouseButtonClick = MyGUI::newDelegate( this, &MainMenuState::OnClickLoadGameOkOrCancel );
    _button = m_pGui->findWidget<MyGUI::Button>( "btLoadGameCancel" );
    _button->eventMouseButtonClick = MyGUI::newDelegate( this, &MainMenuState::OnClickLoadGameOkOrCancel );
    // Опции
    _button = m_pGui->findWidget<MyGUI::Button>( "btOptionOk" );
    _button->eventMouseButtonClick = MyGUI::newDelegate( this, &MainMenuState::OnClickOptionOkOrCancel );
    _button = m_pGui->findWidget<MyGUI::Button>( "btOptionCancel" );
    _button->eventMouseButtonClick = MyGUI::newDelegate( this, &MainMenuState::OnClickOptionOkOrCancel );
    _combobox = m_pGui->findWidget<MyGUI::ComboBox>( "cbLanguage" );
    GameManager::getSingleton().getOptionParser()->getString("language");
    _combobox->setIndexSelected( GameManager::getSingleton().getOptionParser()->getInt("language") );
    _combobox->eventComboAccept = MyGUI::newDelegate( this, &MainMenuState::OnComboAcceptOptionLanguage );
    // Авторы
    // TODO
    // установка языка
    if( GameManager::getSingleton().getSpracheParser()->isGeladen() )
    {
    	Parser* _parser = GameManager::getSingleton().getSpracheParser();
    	_parser->prepareMyGUI( m_pGui );
    }
}
/*
 * -----NEW GAME
 */
void MainMenuState::OnClickNewGame( MyGUI::WidgetPtr _sender )
{
	m_pGui->findWidgetT( "wndMainmenu" )->setVisible(false);
	m_pGui->findWidgetT( "wndNewGame" )->setVisible(true);
}
void MainMenuState::OnClickNewGameOkOrCancel( MyGUI::WidgetPtr _sender )
{
	if( _sender->getName() == "btNewGameOk")
	{
		// TODO btNewGameOk
		return;
	}
	else if( _sender->getName() == "btNewGameCancel" )
	{
		// TODO btNewGameCancel
		return;
	}
}
/*
 * -----LOAD GAME
 */
void MainMenuState::OnClickLoadGame( MyGUI::WidgetPtr _sender )
{
	// TODO OnClickLoadGame
}
void MainMenuState::OnClickLoadGameOkOrCancel( MyGUI::WidgetPtr _sender )
{
	// TODO OnClickLoadGameOkOrCancel
}
/*
 * ------OPTION
 */
void MainMenuState::OnClickOption( MyGUI::WidgetPtr _sender )
{
	m_pGui->findWidgetT( "wndMainmenu" )->setVisible(false);
	m_pGui->findWidgetT( "wndOption" )->setVisible(true);
	// создать копию настроек
	GameManager::getSingleton().getOptionParser()->SaveTmp();
}
void MainMenuState::OnClickOptionOkOrCancel( MyGUI::WidgetPtr _sender )
{
	m_pGui->findWidgetT( "wndMainmenu" )->setVisible( true );
	m_pGui->findWidgetT( "wndOption" )->setVisible( false );
	if( _sender->getName() == "btOptionOk")
	{
		try{
			GameManager::getSingleton().getOptionParser()->SaveElements();
			GameManager::getSingleton().LoadSprache();
		}
		catch(SwsoException e)
		{
			cout << e.m_strMessage << endl;
		}
		return;
	}
	else if( _sender->getName() == "btOptionCancel" )
	{
		GameManager::getSingleton().getOptionParser()->LoadTmp();
		return;
	}

}
void MainMenuState::OnComboAcceptOptionLanguage( MyGUI::ComboBoxPtr _sender, size_t _index )
{
	GameManager::getSingleton().getOptionParser()->setValue( "language", _index );
}
/*
 * -----CREDIT
 */
void MainMenuState::OnClickCredit( MyGUI::WidgetPtr _sender )
{
	// TODO DEBUG
	try
	{
		m_pItems->DebugSave();
	}
	catch( SwsoException e )
	{
		cout << e.m_strMessage << endl;
	}

	// TODO OnClickCredit
}
/*
 * -----QUIT
 */
void MainMenuState::OnClickQuit( MyGUI::WidgetPtr _sender )
{
	m_bExitGame = true;
}
